﻿package Lii.FTG.Key
{
	import Lii.arithmetic.Math.Combination;
	import Lii.events.FTGKeyEvent;
	
	import flash.events.EventDispatcher;

	public class AtkKey	 extends EventDispatcher{		
		protected var _keyCatch:KeyCatch
		protected var _keyGroupName:String
		protected var _keyGroupID:uint
		protected var _keyCombList:Array=new Array
		protected var _keyComb:Combination	
		//
		protected var _keyList:Array=new Array
		protected var _length:uint=0
		//
		public function AtkKey(keyCatch:KeyCatch,...keys){
			this._keyCatch=keyCatch			
			for(var i:uint=0;i<keys.length;i++){
				if(keys[i] is LiiKey){
					this._keyList.push(keys[i])					
				}else{
					throw new Error("[Lii.FTG.Key.AtkKey]无法把"+typeof(keys[i])+"转化为LiiKey类")
				}
			}
			//
			this._length=this._keyList.length
			this._keyGroupName=this._keyCatch.getNewGroupName()
			this._keyGroupID=this._keyCatch.getGroupID(this._keyGroupName)
			//
			var Keys:Array=new Array()
			for(i=0;i<this._length;i++){
				this._keyList[i].combined=true
				this._keyList[i].groupName=this._keyGroupName
				this._keyList[i].groupID=this._keyGroupID
				this._keyList[i].disable=true
				this._keyCatch.addKey(this._keyList[i])
				Keys.push(this._keyList[i].keyName)
			}
			//
			this._keyComb=new Combination(Keys)
			Keys=this._keyComb.getList()			
			for(i=0;i<Keys.length;i++){
				this._keyCombList[Keys[i].code]=Keys[i].name			
			}				
			//
			this._keyCatch.addEventListener(FTGKeyEvent.KEY_UP,_onKeyUp)
			this._keyCatch.addEventListener(FTGKeyEvent.KEY_DOWN,_onKeyDown)	
			this._keyCatch.addEventListener(FTGKeyEvent.KEY_HOLD,_onKeyHold)
		}
		public function start():void{
			for(var i:uint=0;i<this._length;i++){
				this._keyList[i].disable=false
			}
			this.dispatchEvent(new FTGKeyEvent(FTGKeyEvent.CROSS_START))
		}
		public function stop():void{
			for(var i:uint=0;i<this._length;i++){
				this._keyList[i].disable=true
			}
			this.dispatchEvent(new FTGKeyEvent(FTGKeyEvent.CROSS_STOP))
		}
		protected function _onKeyUp(e:FTGKeyEvent):void{
			if(e.groupID==this._keyGroupID){
				var De:FTGKeyEvent=new FTGKeyEvent(e.type)
				De.keyCode=e.keyCode
				De.keyName=this._keyCombList[e.keyCode]
				De.holdTime=e.holdTime
				De.voidTime=e.voidTime
				De.groupName=e.groupName
				De.groupID=e.groupID								
				this.dispatchEvent(De)				
			}
		}
		protected function _onKeyDown(e:FTGKeyEvent):void{
			if(e.groupID==this._keyGroupID){
				var De:FTGKeyEvent=new FTGKeyEvent(e.type)
				De.keyCode=e.keyCode
				De.keyName=this._keyCombList[e.keyCode]
				De.holdTime=e.holdTime
				De.voidTime=e.voidTime
				De.groupName=e.groupName
				De.groupID=e.groupID			
				this.dispatchEvent(De)								
			}
		}
		protected function _onKeyHold(e:FTGKeyEvent):void{
			if(e.groupID==this._keyGroupID){						
				var De:FTGKeyEvent=new FTGKeyEvent(e.type)
				De.keyCode=e.keyCode
				De.keyName=this._keyCombList[e.keyCode]
				De.holdTime=e.holdTime
				De.voidTime=e.voidTime
				De.groupName=e.groupName
				De.groupID=e.groupID				
				this.dispatchEvent(De)
			}
		}		
	}
}